//
//  WaiLiMianMuQiangViewController.m
//  Qihuo
//
//  Created by Cheng Qi on 13-12-10.
//  Copyright (c) 2013年 Vencoo. All rights reserved.
//

#import "WaiLiMianMuQiangViewController.h"

@interface WaiLiMianMuQiangViewController ()

@end

@implementation WaiLiMianMuQiangViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    self.sendCheckDatas =[NSMutableArray array];
    _existBtnArray =[[NSMutableArray alloc]initWithCapacity:0];
    _noBtnArray =[[NSMutableArray alloc]initWithCapacity:0];
    if ([Appinfomation shareIstance].strongXunJianCount == 4)
    {
        self.title = @"外立面幕墙自查";
        self.sendUrl = addWailiMianmu;
    }
    else if ([Appinfomation shareIstance].strongXunJianCount == 5)
    {
        self.title = @"外立面幕墙建筑特殊气候自查";
        self.sendUrl = addWaiLiMianMuQiHou;
    }
    [super viewDidLoad];
    
    [self initData];
    self.floorNames = [NSMutableArray array];
    self.floorIds = [NSMutableArray array];
    for (NSDictionary *dic in self.allShowDataArr)
    {
        [self.floorNames addObject:[dic valueForKey:@"floorName"]];
        [self.sendFloorDatas addObject:@""];
        [self.floorIds addObject:[NSString stringWithFormat:@"%d",[self.allShowDataArr indexOfObjectss:dic]+1]];
        [self.xunJianCellInfos addObject:@{@"hidden":@"1",@"floorId":[NSString stringWithFormat:@"%d",[self.allShowDataArr indexOfObjectss:dic]+1]}];
    }
    [self initData];
    [self showUIWithArray:self.allShowDataArr andFloorId:@"1"];
    NSIndexPath *index = [NSIndexPath indexPathForRow:0 inSection:0];
    [self.myTableView selectRowAtIndexPath:index animated:YES scrollPosition:UITableViewScrollPositionMiddle];

}

#pragma mark--Resource
-(void)resource
{
    [_existBtnArray removeAllObjects];
    [_noBtnArray removeAllObjects];
    NSLog(@"%@",self.floorIdQuanju);
    if ([self.floorIdQuanju isEqualToString:@"7"])
    {
        for (int i=1; i<5; i++) {
            UIButton *btn =(UIButton*)[_view6 viewWithTag:i];
            [btn addTarget:self action:@selector(myBtn:) forControlEvents:UIControlEventTouchUpInside];
            btn.tag=i;
            [_existBtnArray addObject:btn];
            UIButton *btnG =(UIButton*)[_view6 viewWithTag:11*i];
            [btnG addTarget:self action:@selector(myBtn:) forControlEvents:UIControlEventTouchUpInside];
            btnG.tag=i;
            [_noBtnArray addObject:btnG];
        }
        for (int i=5; i<7; i++) {
            UITextField *field =(UITextField*)[_view6 viewWithTag:i];
            field.tag =i;
        }
    }
    if ([self.floorIdQuanju isEqualToString:@"8"]) {
        for (int i=1; i<6; i++) {
            UIButton *btn =(UIButton*)[_view7 viewWithTag:i];
            [btn addTarget:self action:@selector(myBtn:) forControlEvents:UIControlEventTouchUpInside];
            btn.tag=i;
            [_existBtnArray addObject:btn];
            UIButton *btnG =(UIButton*)[_view7 viewWithTag:11*i];
            [btnG addTarget:self action:@selector(myBtn:) forControlEvents:UIControlEventTouchUpInside];
            btnG.tag=i;
            [_noBtnArray addObject:btnG];
        }
    }
    if ([self.floorIdQuanju isEqualToString:@"9"]) {
        for (int i=1; i<4; i++) {
            UITextField *field =(UITextField*)[_view8 viewWithTag:i];
            field.tag =i;
        }
    }
    
}
#pragma mark--Action
-(void)textFieldDidEndEditing:(UITextField *)textField
{
    if ([self.floorIdQuanju integerValue]>= 7)
    {
        NSMutableDictionary *dic =[[NSMutableDictionary alloc]initWithCapacity:0];
        [dic setObject:[NSString stringWithFormat:@"%d",textField.tag] forKey:@"checkId"];
        [dic setObject:[textField.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] forKey:@"result"];
        [self.sendCheckDatas replaceObjectAtIndex:textField.tag-1 withObject:dic];
    }
    else
    {
        [self.sendCheckDatas replaceObjectAtIndex:textField.tag - 100 withObject:@{@"checkId":[NSString stringWithFormat:@"%i",textField.tag - 99],@"result":[textField.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]}];
    }
    NSLog(@"%@",self.sendCheckDatas);
}
-(void)myBtn:(UIButton*)send
{
    NSMutableDictionary *dic =[[NSMutableDictionary alloc]initWithCapacity:0];
    NSString *str =@"无";
    if (send ==[_existBtnArray objectAtIndex:send.tag-1]) {
        [send setImage:[UIImage imageNamed:@"选中.png"] forState:UIControlStateNormal];
        UIButton *btn =[_noBtnArray objectAtIndex:send.tag-1];
        [btn setImage:[UIImage imageNamed:@"未选中.png"] forState:UIControlStateNormal];
        str =@"有";
    }
    if (send ==[_noBtnArray objectAtIndex:send.tag-1]) {
        [send setImage:[UIImage imageNamed:@"选中.png"] forState:UIControlStateNormal];
        UIButton *btn =[_existBtnArray objectAtIndex:send.tag-1];
        [btn setImage:[UIImage imageNamed:@"未选中.png"] forState:UIControlStateNormal];
        str =@"无";
    }
    [dic setObject:[NSString stringWithFormat:@"%d",send.tag] forKey:@"checkId"];
    [dic setObject:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] forKey:@"result"];
    [self.sendCheckDatas replaceObjectAtIndex:send.tag-1 withObject:dic];
    NSLog(@"%@",self.sendCheckDatas);
}
- (void)initData
{
    NSArray *checkNames = @[@"有无破损：",@"位置：",@"数量："];
    NSDictionary *glass1 = @{@"floorName": @"玻璃（室外）",@"checkNames":checkNames};
    NSDictionary *stoneCai1 = @{@"floorName": @"石材（室外）",@"checkNames":checkNames};
    NSDictionary *aluCai1 = @{@"floorName": @"铝材（室外）",@"checkNames":checkNames};
    NSDictionary *mifengJiao1 = @{@"floorName": @"密封胶(室外)",@"checkNames":checkNames};
    NSDictionary *glass2 = @{@"floorName": @"玻璃（室内）",@"checkNames":checkNames};
    NSDictionary *aluCai2 = @{@"floorName": @"铝材（室内）",@"checkNames":checkNames};
    NSDictionary *mifengJiao2 = @{@"floorName": @"密封胶（室内）"};
    NSDictionary *shouligujian = @{@"floorName": @"受力固件"};
    NSDictionary *yushui = @{@"floorName": @"雨水渗透"};
    
    if ([Appinfomation shareIstance].strongXunJianCount == 4)
    {
        self.allShowDataArr = @[glass1,stoneCai1,aluCai1,mifengJiao1,glass2,aluCai2,mifengJiao2,shouligujian,yushui];
    }
    else if([Appinfomation shareIstance].strongXunJianCount == 5)
    {
        self.allShowDataArr = @[glass1,stoneCai1,aluCai1,mifengJiao1];
    }
}
- (void)showUIWithArray:(NSArray *)array andFloorId:(NSString *)floorId
{
    [self clearAll];
    self.floorIdQuanju = floorId;
    id receiveFloorObj = [self.sendFloorDatas objectAtIndex:[self.floorIds indexOfObjectss:self.floorIdQuanju]];
    if (![receiveFloorObj isKindOfClass:[NSString class]])
    {
        self.sendCheckDatas = [receiveFloorObj objectForKey:@"checkData"];
    }
    else
    {
        [self.sendCheckDatas removeAllObjects];
        if ([floorId integerValue] <= 6)
        {
            [self.sendCheckDatas addObjectsFromArray:@[@{@"checkId": @"1",@"result":@"3"},@{@"checkId": @"2",@"result":@""},@{@"checkId": @"3",@"result":@""}]];
        }
        else if ([floorId integerValue] == 7)
        {
            [self.sendCheckDatas addObjectsFromArray:@[@{@"checkId":@"1",@"result":@""},@{@"checkId":@"2",@"result":@""},@{@"checkId":@"3",@"result":@""},@{@"checkId":@"4",@"result":@""},@{@"checkId":@"5",@"result":@""},@{@"checkId":@"6",@"result":@""}]];
        }
        else if ([floorId integerValue] == 8)
        {
            [self.sendCheckDatas addObjectsFromArray:@[@{@"checkId":@"1",@"result":@""},@{@"checkId":@"2",@"result":@""},@{@"checkId":@"3",@"result":@""},@{@"checkId":@"4",@"result":@""},@{@"checkId":@"5",@"result":@""}]];
        }
        else
        {
            [self.sendCheckDatas addObjectsFromArray:@[@{@"checkId":@"1",@"result":@""},@{@"checkId":@"2",@"result":@""},@{@"checkId":@"3",@"result":@""}]];
        }
    }
    if ([floorId integerValue] <= 6)
    {
        NSDictionary *arrayDic = [array firstObject];
        NSArray *checkNames = [arrayDic objectForKey:@"checkNames"];
        for (int j = 0; j < checkNames.count; j++)
        {
            NSString *checkName = [checkNames objectAtIndex:j];
            UILabel *checkNameLb = [UILabel new];
            checkNameLb.text = checkName;
            checkNameLb.font = [UIFont systemFontOfSize:25];
            CGSize MYSZ = [checkNameLb.text sizeWithFont:checkNameLb.font constrainedToSize:CGSizeMake(MAXFLOAT, 40)];
            checkNameLb.frame = CGRectMake(10, 10 + (MYSZ.height + interval111) * j, MYSZ.width, MYSZ.height);
            [self.backScrollV addSubview:checkNameLb];
            [self.controlWillClears addObject:checkNameLb];
            if (j == 0)
            {
                [self addSingleSelect:@[@"有",@"无"]andResult:[[self.sendCheckDatas objectAtIndex:j]valueForKey:@"result"]];
            }
            else
            {
                UITextField *checkTf = [[UITextField alloc]initWithFrame:CGRectMake(checkNameLb.frame.origin.x + checkNameLb.frame.size.width, checkNameLb.frame.origin.y, 300, MYSZ.height)];
                checkTf.text = [[[self.sendCheckDatas objectAtIndex:j]valueForKey:@"result"] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
                checkTf.tag = 100+j;
                checkTf.delegate = self;
                checkTf.background = [UIImage imageNamed:@"输入框底"];
                [self.backScrollV addSubview:checkTf];
                [self.controlWillClears addObject:checkTf];
            }
        }
    }
    else if([floorId integerValue]==7)
    {
        _view6 =[[[NSBundle mainBundle]loadNibNamed:@"WaiLiMianMuQiang" owner:self options:nil]lastObject];
        [self.backScrollV addSubview:_view6];
        [self.controlWillClears addObject:_view6];
    }else if([floorId integerValue]==8){
        _view7 =[[[NSBundle mainBundle]loadNibNamed:@"WaiLiMianSecond" owner:self options:nil]lastObject];
        [self.backScrollV addSubview:_view7];
        [self.controlWillClears addObject:_view7];
    }
    else
    {
        _view8 =[[[NSBundle mainBundle]loadNibNamed:@"WaiLiMianThird" owner:self options:nil]lastObject];
        [self.backScrollV addSubview:_view8];
        [self.controlWillClears addObject:_view8];
    }
    if (![receiveFloorObj isKindOfClass:[NSString class]])
    {
        [self addCommentKuang:[receiveFloorObj objectForKey:@"floorRemark"]];
    }
    else
    {
        [self addCommentKuang:@""];
    }
}
- (void)selectOptions:(id)sender//工程部
{
    UIButton *btn = (UIButton*)sender;
    for (int i = 0;i<self.selectBtnArr.count;i++)
    {
        NSArray *btnArr = [self.selectBtnArr objectAtIndex:i];
        if ([btnArr containsObject:btn])
        {
            for (int j = 0;j<btnArr.count;j++)
            {
                UIButton *seleBtn = [btnArr objectAtIndex:j];
                [seleBtn setImage:self.roundUnselect forState:UIControlStateNormal];
                if (btn == [btnArr objectAtIndex:j])
                {
                    [seleBtn setImage:self.roundSelect forState:UIControlStateNormal];
                    [self.seleBtnData replaceObjectAtIndex:i withObject:[NSString stringWithFormat:@"%d",j]];
//                    [isNormals replaceObjectAtIndex:i+1 withObject:[NSString stringWithFormat:@"%d",j]];
//                    UITextField *tf = [remarkTFs objectAtIndex:i+1];
//                    if (j == 0)
//                    {
//                        [[[UIApplication sharedApplication]keyWindow]endEditing:YES];
//                        tf.text = @"";
//                    }
//                    else if(j == 1)
//                    {
//                        [tf becomeFirstResponder];
//                    }
                    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"%i",i+1],@"checkId",[self.seleBtnData objectAtIndex:i],@"result",nil];
                    [self.sendCheckDatas replaceObjectAtIndex:i withObject:dic];
                }
            }
        }
    }
    NSLog( @"%@",self.sendCheckDatas);
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.myTableView) {
        return self.floorNames.count;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    [super tableView:tableView cellForRowAtIndexPath:indexPath];
    if (tableView == self.myTableView)
    {
        self.mycell.textLabel.text = [self.floorNames objectAtIndex:indexPath.row];
        return self.mycell;
    }
    return nil;
}

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self hiddenAll];
    if (tableView == self.myTableView)
    {
        [self showUIWithArray:self.allShowDataArr andFloorId:[self.floorIds objectAtIndex:indexPath.row]];
    }
       [self resource];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
